import { Component, OnInit } from '@angular/core';
import { MemberService } from '../member.server';
import { Router } from '@angular/router';
import { LayoutRourteStatus } from 'src/app/model/status';
import { MessageService, HUAN_ZHE_A, HUAN_ZHE_B, HUAN_ZHE_C, PHONE } from 'src/app/core/service';
import { FormGroup, FormBuilder } from '@angular/forms';
import { TradeType, TradeTypes, formatDateGlob } from '../pipe-member/pipe.member';
import { cellConfig } from 'src/app/components/table-basic/table-basic';
import { NzModalService, NzModalComponent } from 'ng-zorro-antd';
import { ApiService } from 'src/app/core/api';
import { fromEvent } from 'rxjs';

@Component({
    selector: 'app-member-center-transfer',
    templateUrl: './member-center-transfer.component.html',
    styleUrls: ['./member-center-transfer.component.scss']
})
export class MemberCenterTransferComponent implements OnInit {
    private _layoutRourteStatus: LayoutRourteStatus = {
        manu2Url: 'true',
        url: 'center/setdzcf',
        breadcrumbs: [{ tragetTitle: '电子处方列表' }]
    };
    validateForm: FormGroup;
    visible: boolean = false;
    isSpinning: boolean = false;
    isOver: boolean = false;
    tradeTypes: TradeType[] = TradeTypes;

    private _msgList: Array<any>;
    private _data: Array<any> = [];
    private _dataY: Array<any> = [];
    private ksSel: string[] = ['0-0-1'];
    private _totalMap: any;

    private _config: Array<cellConfig> = [
        {
            name: '操作',
            key: 'cz',
            labels: [
                {
                    name: '查看',
                    type: 'click'
                }
            ]
        },
        {
            name: '处方号',
            key: 'cfhm'
        }, {
            name: '开方时间',
            key: 'kfsj',
        }, {
            name: '就诊卡号',
            key: 'jzkh',
        }, {
            name: '医生',
            key: 'ys'
        }, {
            name: '患者姓名',
            key: 'hzxm'
        }, {
            name: '状态',
            key: 'zt'
        }
    ];

    private _page: any = {
    };
    private isshow = false;
    private isDKbool;
    private isJy;
    ngOnInit() {
        this.msg.sendLayoutRourteStatus(this._layoutRourteStatus);
        this.resetForm();
        this.loadList('商户提现列表');
        this.msg.getMessage().subscribe((e: LayoutRourteStatus) => {
            this.isshow = true;
            if (e['text'] == '读取就诊卡') {
                this.isJy = true;
                this.visible = true;
                this.isDKbool = 'A';
                this._data = HUAN_ZHE_A
                this.seltTdata = this._data[0];
                this.isfx=false;
            } else if (e['text'] == '18221890929') {
                this.isJy = true;
                this.visible = true;
                this.isDKbool = 'C';
                this._data = HUAN_ZHE_C
                this.seltTdata = this._data[0];
                this.isfx = this.seltTdata['fxyy'];
            } else {
                this.isDKbool = 'B';
                this.isJy = false;
                this.visible = true;
                this._data = HUAN_ZHE_B
                this.seltTdata = this._data[0];
                this.isfx=false;
            }
        });
    }
    onKSChange($event: string[]): void {
        console.log($event);
    }


    resetForm(): void {
        this.validateForm = this.fb.group({
            status: ['All'],
            duration: [[]],
            outTradeNo: [null],
            ksSel: [this.ksSel]
        });
        this._page = {
            pageNo: 1,
            pages: 1,
            totalCount: 10
        };
    }
    /**
     * onDtl 查看详情
     */
    private seltTdata: any;
    private isfx=false;
    public onDtl(e): void {
        this.visible = true;
        this.seltTdata = e.data;
        this.isfx = this.seltTdata['fxyy'];

    }
    public close(name: string): void {
        this.visible = false;
        if (name == "签名上传") {
           this.isSpinning = true;
           window.postMessage({'message': 'signature', id: this.seltTdata.cfhm }, '*');
        }
    }
    /**
     * cancelTransfer 取消申请
     */
    public cancelTransfer(e): void {
        this.isSpinning = true;
        this.time();
    }
    public time(): void {
        setTimeout(() => {
            this.isOver = true;
        }, 1200);
        setTimeout(() => {
            this.isSpinning = false;
            switch (this.isDKbool) {
                case 'A':
                    HUAN_ZHE_A[0].zt = '已提交'
                    this._data = HUAN_ZHE_A;

                    break;
                case 'B':
                    HUAN_ZHE_B[0].zt = '驳回'
                    this._data = HUAN_ZHE_B;
                    break;
                case 'C':
                    HUAN_ZHE_C[0].zt = '已提交'
                    this._data = HUAN_ZHE_C;
                    break;
                default:
                    break;
            }
        }, 1400);
    }

    public onListClick(e): void {
        e.name === '查看' ? this.onDtl(e) : this.cancelTransfer(e);
    }
    public onSearch(type: string): void {
    }

    async loadList(url) {
    }

    public successListOk(data, url): void {
    }
    constructor(
        private memberServer: MemberService,
        private api: ApiService,
        private router: Router,
        private msg: MessageService,
        private fb: FormBuilder,
        private modalService: NzModalService
    ) { 
        fromEvent(window, 'message').subscribe((event) => {
            console.log(event)
            if(event['data'].message=="signsuccess") {
                this.isSpinning = true;
                this.time();
                this.api.get('http://admin.wholesale.zhaioto.com/Public/login', { ['mobile']: PHONE , ['type']: '3'}).subscribe((e: LayoutRourteStatus) => {
              
                  });
        　　}else if(event['data'].message=="signfail"){
                this.visible = false;
                this.isSpinning = false;
            }
        });
    }
}
